#include <iostream>
#include <sstream>

using namespace std;

string Name, Studentnr  = ""; 
int* RandomNumbers = new int[20];

bool HasDuplicate(int x)
{
	bool isDuplicate = false;

	for(int i=1; i<=20; i++)
	{
		if(*(RandomNumbers+i) == x)
			isDuplicate = true;
	}

	return isDuplicate;
}

void FillArray()
{
	for(int i=0; i<=20; i++)
	{
		*(RandomNumbers+i) = 0;
	}

	int x = rand() % 1000 + 100;

	for(int i=1; i<=20; i++)
	{		
		while(HasDuplicate(x))
		{
			x = rand() % 1000 + 100;
		}

		*(RandomNumbers+i) = x;		
	}
}

void SortArray()
{
	int temp;   

	for(int i = 1; i <= 20; i++)
	{		
		for (int j=1; j <= 19; j++)
		{
			if (*(RandomNumbers +j+1) < *(RandomNumbers +j)) 
			{ 
				temp = *(RandomNumbers +j);
				*(RandomNumbers +j) = *(RandomNumbers +j+1);
				*(RandomNumbers +j+1) = temp;			
			}
		}
	}
}

void PrintAsc()
{
	for(int i = 0; i<=3; i++)
	{
		for(int j = 1; j <=5; j++)
		{
			cout << *(RandomNumbers + (j+(5*i))) << " ";
		}
		cout << endl;
	}
	cout << endl;
}

void PrintDes()
{
	for(int i = 0; i<=3; i++)
	{
		for(int j = 1; j <=5; j++)
		{
			cout << *(RandomNumbers + (21-(j+(5*i)))) << " ";
		}
		cout << endl;
	}
	cout << endl;
}

void PrintAscTotal()
{
	for(int i = 0; i<=3; i++)
	{
		int x = 0;
		for(int j = 1; j <=5; j++)
		{
			cout << *(RandomNumbers + (j+(5*i))) << " ";
			x += *(RandomNumbers + (j+(5*i)));
		}
		cout << "Avg: "<< x/5 << endl;
	}
	cout << endl;
}

void Print()
{
	for(int i=1; i<20; i++)
	{
		cout << RandomNumbers[i] << endl;
	}

}

int main()
{
	cout << "Enter your name please: ";
	cin >> Name;
	cout << "Enter your student number please: ";
	cin >> Studentnr;
	
	FillArray();
	
	SortArray();	
	
	PrintAsc();
	PrintDes();
	PrintAscTotal();
	
	stringstream ss;
	ss << "Bye " << Name.c_str() << ", " << Studentnr.c_str() << " cya later, take care bro." << endl;
	cout << ss.str() << endl;

	cin >> Name;

	return 0;
}